#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <set>
using namespace std;
typedef long long LL;
const int MAXN=1e5+7;
LL a[MAXN];
int main() {
    cin.sync_with_stdio(false);
    LL n,k,ans;
    cin>>n>>k;
    for (int i=0; i<n; ++i) cin>>a[i];
    priority_queue<LL, vector<LL>, greater<LL> > q;
    int cnt=0;
    ans=0;
    for (int i=0; i<n; ++i) {
        if (cnt>=k) {
            q.push(a[i]);
            cnt-=k;
        }
        else {
            if (!q.empty() && a[i]>q.top()) {
                //cout<<q.top()<<endl;
                ans=ans+q.top();
                cnt++;
                q.pop(); q.push(a[i]);
            }
            else {
                cnt++;
                ans+=a[i];
            }
        }
        //cout<<"ans: "<<ans<<endl;
    }
    printf("%lld\n", ans);
    return 0;
}
